//
//  AddConsigneVC.m
//  ProjectforHDX
//
//  Created by Jaesun on 16/8/1.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "AddConsigneVC.h"

@interface AddConsigneVC ()
/**
 *  姓名
 */
@property (nonatomic, strong) RZCustomTextField *nameTxtField;
/**
 *  手机号
 */
@property (nonatomic, strong) RZCustomTextField *telTxtField;
/**
 *  保存
 */
@property (nonatomic, strong) RZCustomButtom *saveBtn;

@property (nonatomic, strong) NSMutableDictionary *postDic;

@end

@implementation AddConsigneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupDatas];
    [self setupViews];
}

#pragma mark 数据初始化
- (void)setupDatas {
    
}

#pragma mark 视图初始化
- (void)setupViews {
    
    self.mBackColor = [UtilCheck hexStringToColor:@"f6f7fb" alpha:1];
    self.labnav.textColor = [UIColor whiteColor];
    self.labnav.text = [UtilCheck language:@"setting.addConsignee"];
    self.labnav.backgroundColor = [UIColor clearColor];
    
    if (!self.isAddConsigne) {
        self.labnav.text = [UtilCheck language:@"setting.updateConsignee"];
    }
    UIView *nameBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    nameBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:nameBGView];
    
    self.nameTxtField = [[RZCustomTextField alloc] initWithFrame:CGRectMake(5, 0, SCREEN_WIDTH - 15, 50)];
    self.nameTxtField.keyboardType = UIKeyboardTypeDefault;

    self.nameTxtField.placeholder = [UtilCheck language:@"setting.name"];
    if (![UtilCheck DataIsNuLL:[self.consigneDic objectForKey:@"username"]]) {
        self.nameTxtField.text = [self.consigneDic objectForKey:@"username"];
    }
    [self.view addSubview:self.nameTxtField];
    
    UIView *telBGView = [[UIView alloc] initWithFrame:CGRectMake(0, self.nameTxtField.bottom + 0.5, SCREEN_WIDTH, 50)];
    telBGView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:telBGView];
    
    self.telTxtField = [[RZCustomTextField alloc] initWithFrame:CGRectMake(5, self.nameTxtField.bottom + 0.5, SCREEN_WIDTH- 15, 50)];
    self.telTxtField.keyboardType = UIKeyboardTypeNumberPad;
    self.telTxtField.placeholder = [UtilCheck language:@"setting.tel"];
    if (![UtilCheck DataIsNuLL: [self.consigneDic objectForKey:@"tel"]]) {
        self.telTxtField.text = [self.consigneDic objectForKey:@"tel"];
    }
    [self.view addSubview:self.telTxtField];
    
    self.saveBtn = [[RZCustomButtom alloc] initWithFrame:CGRectMake(20, self.telTxtField.bottom + 20, SCREEN_WIDTH - 40, 40)];
    self.saveBtn.layer.cornerRadius = 5;
    self.saveBtn.backgroundColor = [UtilCheck hexStringToColor:@"6984e7" alpha:1];
    [self.saveBtn setTitle:[UtilCheck language:@"setting.save"] forState:(UIControlStateNormal)];
    [self.saveBtn addTarget:self action:@selector(touchSaveBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.saveBtn];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark- 点击事件
#pragma mark-
/**
 *  保存
 *
 *  @param sender
 */
- (void)touchSaveBtn:(RZCustomButtom *)sender {
  
    NSString *msg = [self isCanSave];
    if (![UtilCheck DataIsNuLL:msg]) {
        [[BDHUD sharedManager] showWords:msg];
        return;
    }
    else {
        self.postDic = [NSMutableDictionary dictionary];
        [self.postDic setObject:self.telTxtField.text forKey:@"tel"];
        [self.postDic setObject:self.nameTxtField.text forKey:@"username"];
    }
    
    if (self.isAddConsigne) {
        [self insertConsignee];
    }
    else {
        [self.postDic setObject:[self.consigneDic objectForKey:@"id"] forKey:@"id"];
        [self updateConsignee];
    }
}

#pragma mark- 网络请求
#pragma mark-

#pragma mark 新建收货联系人
- (void)insertConsignee {
    
    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    [AFHttpTool postJsonDatawithUrl:@"insertConsignee"  parameters:self.postDic success:^(id responseObject) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if(1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]) {
            
            NSString *info = [[responseObject objectForKey:@"head"] objectForKey:@"info"];
            
            if ([UtilCheck DataIsNuLL:info]) {
                // 新建收货联系人成功
                info = [UtilCheck language:@"setting.cogeAdd"];
            }
            [[BDHUD sharedManager] showWords:info];
            [self.navigationController popViewControllerAnimated:YES];

        }
        else {
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
        }
        
    } fail:^(id responseObject, NSError *error) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];
}

#pragma mark 修改收货联系人
- (void)updateConsignee {
    
    [[BDHUD sharedManager] showBDHUD:self.view title:nil];
    [AFHttpTool postJsonDatawithUrl:@"updateConsignee"  parameters:self.postDic success:^(id responseObject) {
        
        [[BDHUD sharedManager] dismissHUD];
        
        if(1 == [[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]) {
            
            NSString *info = [[responseObject objectForKey:@"head"] objectForKey:@"info"];
            
            if ([UtilCheck DataIsNuLL:info]) {
                // 修改收货联系人成功
                info = [UtilCheck language:@"setting.cogeUpdate"];
            }
            [[BDHUD sharedManager] showWords:info];
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
        }
        
    } fail:^(id responseObject, NSError *error) {
        
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
        
    }];
}

#pragma mark- 其他事件

/**
 *  检查数据是否能够保持到服务器
 *
 *  @return 不能保存的原因
 */
- (NSString *)isCanSave{
    if ([UtilCheck DataIsNuLL:self.nameTxtField.text]) {
        
        return [UtilCheck language:@"setting.cogeName"]; // 收货联系人姓名为空
    }
    else if ([UtilCheck DataIsNuLL:self.telTxtField.text]){
        
        return [UtilCheck language:@"setting.cogeTel"];// 收货联系人手机号码为空
    }
    else if (![UtilCheck isValidateMobile:self.telTxtField.text]) {
        return [UtilCheck language:@"setting.NOTelNum"]; // 不合法手机号码
    
    }
    return nil;
}

@end
